package edu.ipfw.cs590.cheat.services.mgmt;

/*
 * Course:    CS 590 Web Services
 * Semseter:  Spring 2009
 * Professor: Sedlemeyer
 * Project:   CHEAT (Code Help Educational Assignment Tool)
 * Due Date:  May 1, 2009
 * Team:      Sarcasm
 *            (Baker, Jason; Mitchell, Robin; Shie, Keith)
 *
 * Description:
 * This module is the implementation module for the service contained
 * in the CHEAT System.  This layer will deal only with Service classes
 * and does not reference any of the EJB business/persistence classes.
 * It is responsible for doing any handling of the message in terms of
 * information passed in and out as well as handling SOAPFaults.  It calls
 * a Server class that bridges the gap between Service classes and
 * EJB/Persistence classes.
 *
 * These services cause a SOAPFault to occur if there is a service failure
 * of any kind.  Because of this particular pains have been taken to ensure
 * that null is never returned.
 */



import java.util.List;
import javax.annotation.Resource;
import javax.jws.WebService;
import javax.xml.ws.WebServiceContext;
import edu.ipfw.cs590.cheat.services.AccessRights;
import edu.ipfw.cs590.cheat.services.AssignmentWrapper;
import edu.ipfw.cs590.cheat.services.Keyword;
import edu.ipfw.cs590.cheat.services.RegisteredPerson;
import edu.ipfw.cs590.cheat.services.mgmt.faults.AMSException;
import java.util.ArrayList;
import javax.jws.HandlerChain;
import javax.xml.ws.soap.MTOM;
import org.web_cat._1_0.assignment_sharing.Assignment;


/**
 *
 * @author rrm46582
 */
@HandlerChain(file="handlerchain.xml")
@WebService(serviceName="AssignmentManagementService", portName="AssignmentManagementSOAP",
            targetNamespace="HTTP://cheat.cs590.ipfw.edu",
            endpointInterface="edu.ipfw.cs590.cheat.services.mgmt.AssignmentManagement")
@MTOM

public class AssignmentManagementBean
        extends AssignmentManagementServer
        implements AssignmentManagement

{
	@Resource
	WebServiceContext context;


    public String submitAssignment(String token, byte[] assignmentArchive)
    {
        String returnValue = "";  // Never return null
        try
        {
            System.out.println("###SubmitAssignment Started " + token);
            if (assignmentArchive != null)
            {
                returnValue = this.doSubmitAssignment(token, assignmentArchive);
                //System.out.println("\t->returned uuid = " + returnValue);
            }
            else
            {
                //System.out.println("assignment is null");
            }
        }
        catch(AMSException ex)
        {
            context.getMessageContext().put("FAULT", ex.getMessage());
            System.out.println("###SubmitAssignment " + ex.getMessage());

        }
        finally
        {
            System.out.println("___________________________________\n");
            return returnValue;
        }
    }

    public String replaceAssignment(String token, String replaceUUID, byte[] assignmentArchive)
    {
        String returnValue = ""; // Never return null
        try
        {
            System.out.println("###ReplaceAssignment Started " + replaceUUID + " " + token);
            if (assignmentArchive != null)
            {
                returnValue = this.doReplaceAssignment(token, replaceUUID, assignmentArchive);
            }
        }
        catch(AMSException ex)
        {
            context.getMessageContext().put("FAULT", ex.getMessage());
            System.out.println("###ReplaceAssignment " + ex.getMessage());

        }
        finally
        {
            System.out.println("________________________________\n");
            return returnValue;
        }

    }


    public boolean removeAssignment(String token, String removeUUID)
    {
        boolean returnValue = false;
        try
        {
            System.out.println("###RemoveAssignment Started " + token + " " + removeUUID);
            returnValue = this.doRemoveAssignment(token, removeUUID);
        }
        catch(AMSException ex)
        {
            context.getMessageContext().put("FAULT", ex.getMessage());
            System.out.println("###RemoveAssignment " + ex.getMessage());

        }
        finally
        {
            System.out.println("__________________________________\n");
            return returnValue;
        }
    }


    public byte[] getAssignment(String token, String getUUID)
    {
        byte[] returnValue = new byte[1]; // Never return null
        try
        {
            System.out.println("###GetAssignment Started " + " " + token + " " + getUUID);
            returnValue = doGetAssignment(token, getUUID);
        }
        catch(AMSException ex)
        {
            context.getMessageContext().put("FAULT", ex.getMessage());
            System.out.println("###GetAssignment " + ex.getMessage());

        }
        finally
        {
            System.out.println("________________________________\n");
            return returnValue;
        }

    }





    public boolean register(RegisteredPerson regPerson)
    {
        boolean returnValue = false;
        try
        {
            System.out.println("###Register Started " + regPerson.getEmail());
            returnValue = doRegister(regPerson);
        }
        catch(AMSException ex)
        {
            context.getMessageContext().put("FAULT", ex.getMessage());
            System.out.println("###Register " + ex.getMessage());

        }
        finally
        {
            System.out.println("________________________________\n");
            return returnValue;
        }

    }


    public List<RegisteredPerson> getPendingRegistrationRequests(String secToken)
    {
        // Never return null
        List<RegisteredPerson> returnValue = new ArrayList<RegisteredPerson>();
        try
        {
            System.out.println("###GetPendingRequests Started " + secToken);
            returnValue = doGetPendingRegistrationRequests(secToken);

        }
        catch(AMSException ex)
        {
            context.getMessageContext().put("FAULT", ex.getMessage());
            System.out.println("###GetPendingRequests " + ex.getMessage());

        }
        finally
        {
            System.out.println("________________________________\n");
            return returnValue;
        }
    }


    public boolean grantRegistrationRequest(String secToken, String uuid)
    {
        boolean returnValue = false;
        try
       {
            System.out.println("###GrantRegistration Started " + uuid);
                this.doGrantRegistrationRequest(secToken, uuid);
                returnValue = true;
            //}
       }
        catch(AMSException ex)
        {
            context.getMessageContext().put("FAULT", ex.getMessage());
            System.out.println("###GrantRegistration " + ex.getMessage());

        }
        finally
        {
            System.out.println("________________________________\n");
            return returnValue;
        }

    }



    public boolean denyRegistrationRequest(String secToken, String uuid)
    {
        boolean returnValue = false;
        try
        {

            System.out.println("###DenyRegistration Started " + uuid);
            returnValue = this.doDenyRegistrationRequest(secToken, uuid);
            //returnValue = true;
       }
        catch(AMSException ex)
        {
            context.getMessageContext().put("FAULT", ex.getMessage());
            System.out.println("###DenyRegistration " + ex.getMessage());
        }
        finally
        {
            System.out.println("________________________________\n");
            return returnValue;
        }

    }



    public String login(String userId, String password)
    {
        String returnValue = ""; // Never return null
        try
        {
            System.out.println("###Login Started " + userId + " " + password);
            System.out.println("\t->!!!Remember to take the password out for production!!!");
            returnValue = this.doLogin(userId, password);

        }
        catch(AMSException ex)
        {
            context.getMessageContext().put("FAULT", ex.getMessage());
            System.out.println("###Login " + ex.getMessage());

        }
        finally
        {
            System.out.println("________________________________\n");
            return returnValue;
        }

    }


    public boolean logout(String logoutToken)
    {
        boolean returnValue = false;
        try
        {
            System.out.println("###Logout " + logoutToken);
            returnValue = this.doLogout(logoutToken);
            //returnValue = true;

        }
        catch(AMSException ex)
        {
            context.getMessageContext().put("FAULT", ex.getMessage());
            System.out.println("###Logout " + ex.getMessage());

        }
        finally
        {
            System.out.println("________________________________\n");
            return returnValue;
        }

    }




    public AccessRights getAccessRights(String secToken)
    {
        AccessRights returnValue = new AccessRights(); //never return null
        try
        {
            System.out.println("###GetAccessRights Started " + secToken);
            returnValue = this.doGetAccessRights(secToken);
        }
        catch(AMSException ex)
        {
            context.getMessageContext().put("FAULT", ex.getMessage());
            System.out.println("###GetAccessRights " + ex.getMessage());

        }
        finally
        {
            System.out.println("________________________________\n");
            return returnValue;
        }

    }

    public RegisteredPerson getPerson(String secToken, String email)
    {
        RegisteredPerson returnValue = new RegisteredPerson(); //never return null
        try
        {
            System.out.println("###GetPerson Started " + email);
            returnValue = this.doGetPerson(secToken, email);
        }
        catch(AMSException ex)
        {
            context.getMessageContext().put("FAULT", ex.getMessage());
            System.out.println("###GetPerson " + ex.getMessage());

        }
        finally
        {
            System.out.println("________________________________\n");
            return returnValue;
        }


    }





    public List<Assignment> findAssignmentByCourse(String course)
    {
        List<AssignmentWrapper> assignmentsW;
        //Never return null
        List<Assignment> assignments = new ArrayList<Assignment>();
        try
        {
            System.out.println("###FindByCourse Started " + course);
            assignmentsW = this.doFindAssignmentByCourse(course);
            assignments = this.buildMetaDataList(assignmentsW);
        }
        catch(AMSException ex)
        {
            context.getMessageContext().put("FAULT", ex.getMessage());
            System.out.println("###FindByCource " + ex.getMessage());
        }
        finally
        {
            System.out.println("________________________________\n");
            return assignments;
        }
    }



    public List<Assignment> findAssignmentByKeyword(List<Keyword> keywords)
    {
        List<AssignmentWrapper> assignmentsW;
        //Never return null
        List<Assignment> assignments = new ArrayList<Assignment>();
        try
        {
            System.out.println("###FindByKeyword Started with " + keywords.size() + " keywords");
            assignmentsW = this.doFindAssignmentByKeyword(keywords);
            assignments = this.buildMetaDataList(assignmentsW);
        }
        catch(AMSException ex)
        {
            context.getMessageContext().put("FAULT", ex.getMessage());
            System.out.println("###FindByKeyword " + ex.getMessage());
        }
        finally
        {
            System.out.println("________________________________\n");
            return assignments;
        }
    }



    public List<Assignment> findAssignmentByLanguage(String language)
    {
        List<AssignmentWrapper> assignmentsW;
        //Never return null
        List<Assignment> assignments = new ArrayList<Assignment>();
        try
        {
            System.out.println("###FindByLanguage Started " + language);
            assignmentsW = this.doFindAssignmentByLanguage(language);
            assignments = this.buildMetaDataList(assignmentsW);
        }
        catch(AMSException ex)
        {
            context.getMessageContext().put("FAULT", ex.getMessage());
            System.out.println("###FinddByLanguage " + ex.getMessage());
        }
        finally
        {
            System.out.println("________________________________\n");
            return assignments;
        }
    }

    public List<Assignment> getAssignmentHistory(String assignmentUUID)
    {
        //Never return null
        List<Assignment> assignments = new ArrayList<Assignment>();
        try
        {
            System.out.println("###GetAssignmentHistory Started " + assignmentUUID);
            assignments = this.doGetAssignmentHistory(assignmentUUID);
        }
        catch(AMSException ex)
       {
            context.getMessageContext().put("FAULT", ex.getMessage());
            System.out.println("###GetAssignmentHistory " + ex.getMessage());
        }
        finally
        {
            System.out.println("________________________________\n");
            return assignments;
        }


    }



    public Assignment getAssignmentMetadata(String assignmentUUID)
    {
        Assignment assignment = new Assignment(); //Never return null
        try
        {
            System.out.println("###GetAssignmentMetadata Started " + assignmentUUID);
            assignment = this.doGetAssignmentMetadata(assignmentUUID);
        }
        catch(AMSException ex)
        {
            context.getMessageContext().put("FAULT", ex.getMessage());
            System.out.println("###GetAssignmentMetadata " + ex.getMessage());
        }
        finally
        {
            System.out.println("________________________________\n");
            return assignment;
        }

    }

    public List<Assignment> findAssignmentByAuthor(String author)
    {
        List<AssignmentWrapper> assignmentsW = null;
        //Never return null
        List<Assignment> assignments = new ArrayList<Assignment>();
        try
        {
            System.out.println("###FindByAuthor Started " + author);
            assignmentsW = this.doFindAssignmentByAuthor(author);
            assignments = this.buildMetaDataList(assignmentsW);
        }
        catch(AMSException ex)
        {
            context.getMessageContext().put("FAULT", ex.getMessage());
            System.out.println("###FinddByAuthor " + ex.getMessage());
        }
        finally
        {
            System.out.println("________________________________\n");
            return assignments;
        }
    }

  
    public String getAssignmentOwner(String assignmentUUID) {
        String returnValue = "";
        try
        {
            System.out.println("###GetAssignmentOwner Started " + assignmentUUID);
            returnValue = doGetAssignmentOwner(assignmentUUID);
        } 
        catch (AMSException ex)
        {
            context.getMessageContext().put("FAULT", ex.getMessage());
            System.out.println("###GetAssignmentOwner " + ex.getMessage());
        } 
        finally
        {
            System.out.println("________________________________\n");
            return returnValue;
        }
    }

}
